<?php

/**
 * Contacts main page
 * @package ModuleGestionContacts
 * @version 1.0b
 */

session_start();

// Configuration file
include 'configuration.php';
// Functions
include 'inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	header('Location: index.php');
	exit();
endif;

// Include all objects
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// ---------- START: HEAD
include $configuration['webroot'].'inc/head.php';
// ---------- END: HEAD

// ---------- START: MENU
include $configuration['webroot'].'inc/menu.php';
// ---------- END: MENU

// ---------- START: BODY

if (isset($_GET['sep'])) {
	$company = new Company();
	$company->Get($_GET['sep']);
}
$add = isset($_GET['sep']) ? ' - '.$company->name : '';
?>
<div id="title" style="background-image:url('css/img/contacts.png')">Contacts<?php echo $add; ?></div>

<?php
if (!isset($_GET['operation'])):

	if (!isset($_GET['sep'])) {
		if (empty($_SESSION['search']['contacts']))
			$params = Array(Array('contactId', '>', '0'));
		else 
			$params = Array(Array('lastname', 'LIKE', '%'.$_SESSION['search']['contacts'].'%'));
			
		$contact = new Contact();
		$contactList = $contact->GetList($params, 'lastname');
	} else {
		$gotproroleList = $company->GetGotproroleList();
		$contactList = Array();
		foreach ($gotproroleList as $gotprorole)
			$contactList[] = $gotprorole->GetContact();
			
		$contactList = array_unique($contactList);
			
		if (!empty($_SESSION['search']['contacts'])) {
			$size = count($contactList);
			for ($i = 0; $i < $size; ++$i) {
				if (substr_count(strtolower($contactList[$i]->lastname), strtolower($_SESSION['search']['contacts'])) <= 0)
					unset($contactList[$i]);
			}
		}
	}
?>
	
	<div id="nav">
		<a href="#" id="details" class="nav">Détails</a>
		<span class="separator"></span>
		<a href="contacts.php?operation=add" id="add" class="nav">Nouveau</a>
		<a href="#" id="delete" class="nav">Supprimer</a>
		<span class="separator"></span>
		<a href="#" id="sendmail" class="email">E-mail</a>
		<a href="#" id="merge" class="merge">Fusionner</a>
		<span class="separator"></span>
		<input type="text" id="search-field" class="search-field" value="<?php echo (!empty($_SESSION['search']['contacts'])) ? $_SESSION['search']['contacts'] : ''; ?>" />
		<a href="contacts" id="search" class="search">Rechercher</a>
	</div>
	
	<table cellpadding="0" cellspacing="0" border="0" class="table-objects">
		<tr>
			<th class="check"></th>
			<th>#</th>
			<th>Nom</th>
			<th>E-mail</th>
			<th>Poste</th>
			<th>Adresse</th>
		</tr>
		<?php
		if (count($contactList) > 0):
			foreach ($contactList as $contact):
				$address = $contact->GetAddressList(Array(Array('type', '=', 'MAINADDRESS')));
				if (count($address) > 0) $address = $address[0]; else $address = new Address();
				
				$res  = '<tr>';
				$res .= '<td class="check"><input type="checkbox" value="'.$contact->contactId.'" /></td>';
				$res .= '<td>'.$contact->contactId.'</td>';
				$res .= '<td><strong>'.$contact->lastname.' '.$contact->firstname.'</strong></td>';
				$res .= '<td>'.$contact->email.'</td>';
				$res .= '<td>'.$contact->jobtitle.'</td>';
				if (!empty($address->street)) $res .= '<td>'.$address->number.' '.$address->street.', '.$address->postalcode.' '.$address->city.'</td>'; else $res .= '<td></td>';
				$res .= '</tr>';
				echo $res;
			endforeach;
		else:
			echo '<tr><td colspan="5" class="ui-state-error">Aucune donnée</td></tr>';
		endif;
		?>
	</table>
	
	<div id="sub-nav">
		<a href="#" class="selectall">Tout cocher</a>
		<a href="#" class="selectnone">Tout décocher</a>
	</div>

	<div id="select_model" class="hidden"></div>
<?php
else:
	$contact = new Contact();
	$address = new Address();
	$date = '';
	
	// In case we are in an update state
	if (($_GET['operation'] == 'edit') && isset($_GET['id'])):
		$contact->Get($_GET['id']);
		$address = $contact->GetAddressList(Array(Array('type', '=', 'MAINADDRESS')));
		if (count($address) > 0) $address = $address[0]; else $address = new Address();
		// Format purposes
		if ((strlen($contact->birthdate) > 0) && ($contact->birthdate != '0000-00-00')):
			$date = explode('-', $contact->birthdate);
			$date = $date[2].'/'.$date[1].'/'.$date[0];
		endif;
	endif;

	include 'forms/contact.php';

endif;
// ---------- END: BODY

// ---------- START: FOOT
include $configuration['webroot'].'inc/foot.php';
// ---------- END: FOOT

// ---------- JAVASCRIPTS AND END
include $configuration['webroot'].'inc/js.php'; ?>
<script type="text/javascript" src="js/contacts.js"></script>

</body>
</html>